﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

public partial class Controls_Login : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (null == Session["SignInUser"])
		{
			//this.pnlAnonymous.Visible	= true;
			//this.pnlSignIn.Visible		= false;
		}
		else
		{
			//this.pnlAnonymous.Visible	= false;
			//this.pnlSignIn.Visible		= true;
			//this.ltlNickname.Text		= (Session["SignInUser"] as User).NickName;
		}	
	}



	protected void btnLogout_Click(object sender, EventArgs e)
	{
		Session.Remove("SignInUser");
		HttpCookie theCookie	= new HttpCookie("AutoSignIn");
		theCookie.Expires		= DateTime.Now.AddDays(-1);
		this.Response.Cookies.Add(theCookie);
		//this.pnlAnonymous.Visible	= true;
		//this.pnlSignIn.Visible		= false;
	}

	protected void btnLogin_Click(object sender, ImageClickEventArgs e)
	{

	}

	protected void btnSignIn_Click(object sender, ImageClickEventArgs e)
	{
		UserManager theUserManger			= new UserManager();
		Cosmetic.DataEntity.User theUser	= theUserManger.LoginValidate(this.txtEmail.Text.Trim(), this.txtPassword.Text.Trim());
		if (theUser == null)
		{
			Cosmetic.WebGeneric.WebMessageBox("用户不存在或密码错误!", this.Page);
			//ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Error", "alert('用户不存在或密码错误!');", true);
			Session.Remove("SignInUser");
		}
		else
		{
			Session["SignInUser"]		= theUser;
			//this.pnlAnonymous.Visible	= false;
			//this.pnlSignIn.Visible		= true;
			//this.ltlNickname.Text		= theUser.NickName;

			//if (this.cbSavePassword.Checked)
			{
				HttpCookie theCookie	= new HttpCookie("AutoSignIn");
				theCookie["Email"]		= theUser.Email;
				theCookie["Password"]	= theUser.Password;
				theCookie.Expires		= DateTime.MaxValue;
				this.Response.Cookies.Add(theCookie);
			}
            //Page.RegisterStartupScript("openalert", "<script>alert</script>");
            Response.Redirect("~/Space/BasicInfo.aspx");
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "Register Faild", "alert('登陆成功!')", true);
		}
	}

	protected void Button1_Click(object sender, EventArgs e)
	{
		UserManager theUserManger			= new UserManager();
		Cosmetic.DataEntity.User theUser	= theUserManger.LoginValidate(this.txtEmail.Text.Trim(), this.txtPassword.Text.Trim());
		if (theUser == null)
		{
			Cosmetic.WebGeneric.WebMessageBox("用户不存在或密码错误!", this.Page);
			//ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Error", "alert('用户不存在或密码错误!');", true);
			Session.Remove("SignInUser");
		}
		else
		{
			Session["SignInUser"]		= theUser;
			//this.pnlAnonymous.Visible	= false;
			//this.pnlSignIn.Visible		= true;
			//this.ltlNickname.Text		= theUser.NickName;

			//if (this.cbSavePassword.Checked)
			{
				HttpCookie theCookie	= new HttpCookie("AutoSignIn");
				theCookie["Email"]		= theUser.Email;
				theCookie["Password"]	= theUser.Password;
				theCookie.Expires		= DateTime.MaxValue;
				this.Response.Cookies.Add(theCookie);
			}
		}
	}

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Register/RegistAgreement.aspx");
    }

}
